package com.dao.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import com.bean.LineArticle;
import com.dao.LineArticleDao;
import com.util.CodeUtil;
import com.util.Pager;
@Component
public class LineArticleDaoImpl implements LineArticleDao{

	@Autowired
	private MongoTemplate mongoTemplate;

	@Override
	public Pager<LineArticle> selectLineArticle(Integer curPage,Integer pageSize) {
		Pager<LineArticle> pager=new Pager<LineArticle>();
		if(curPage!=null)
			pager.setCurPage(curPage);
		if(pageSize!=null)
			pager.setPageSize(pageSize);
		Query query=new Query();
		query.skip(pager.getStart()).limit(pager.getPageSize());
		List<LineArticle> lineArticles=mongoTemplate.find(query.with(new Sort(Sort.Direction.DESC,"publishDate")).skip(pager.getStart()).limit(pager.getPageSize()), LineArticle.class);
		List<LineArticle> data=new ArrayList<>();
		List<Integer> indexes=CodeUtil.randomIndexes1(lineArticles.size());
		for(Integer index:indexes){
			data.add(lineArticles.get(index));
		}
		pager.setData(data);
		pager.setTotalRow(Integer.valueOf(String.valueOf(mongoTemplate.count(query, LineArticle.class))));
		return pager;
	}

	
	
}
